package scene;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import geometrie.Point;
import geometrie.Rebouchage;

/**
 * Implementation of a Modele3D that represents a pyramid. It's possible to
 * specify the number of sides of the pyramid. If a big number of sides is
 * specified (around 40, for example) the resulting Modele3D can be seen as a
 * cone.
 */
public class Pyramid implements Modele3D {
	private List<Facette3D> facettes;

	public Iterator<Facette3D> getFacettes() {
		return facettes.listIterator();
	}
	
	public void addTriangulation(Point[] polygon) {
		facettes.addAll(Rebouchage.rebouchageTrouRecCache(polygon));
	}
	/**
	 * Constructs a new pyramid with the specified parameters
	 * 
	 * @param vertex
	 *            the pyramid's vertex
	 * @param height
	 *            the pyramid's height
	 * @param radius
	 *            the pyramid's radius (distance from the bottom polygon to the
	 *            faces)
	 * @param numPoints
	 *            the number of sides. If a big number (around 100) is
	 *            specified, the resulting pyramid can be seen as a cone.
	 */
	public Pyramid(Point center, double height, double radius, int numPoints) {
		double polygonAngle = (numPoints - 2) * 180 / numPoints;
		Point p1, p2, p3;
		double x, y, z;
		x = center.getXCartesien();
		y = center.getYCartesien();
		z = center.getZCartesien();
		p3 = new Point(center.getXCartesien(), center.getYCartesien() + height, center.getZCartesien());
		
		Point[] bottomPolygon = new Point[numPoints];
		
		facettes = new ArrayList<Facette3D>(numPoints + 1);
		int i;
		double radAngle = Math.toRadians(180 - polygonAngle);
		for (i = 0; i != numPoints; i++) {
			p1 = new Point(x + radius * Math.sin(i * radAngle), y, z
					+ radius * Math.cos(i * radAngle));
			p2  = new Point(x + radius * Math.sin((i+1) * radAngle), y, 
								z + radius * Math.cos((i+1) * radAngle));
			
			facettes.add(new Facette3D(p1, p2, p3));
			bottomPolygon[i] = p1;
		}
		addTriangulation(bottomPolygon);
	}

	public String toString() {
		return "Cone [facettes=" + facettes + "]";
	}

}
